package com.zlp.mall.modules.pms.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.pms.dto.req.ProductCategoryAddReq;
import com.zlp.mall.modules.pms.dto.req.ProductCategoryUpdReq;
import com.zlp.mall.modules.pms.dto.resp.ProductCateChildrenResp;
import com.zlp.mall.modules.pms.dto.resp.ProductCategoryResp;
import com.zlp.mall.modules.pms.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/productCategory")
@Api(value = "ProductCategoryController", tags = "后台商品分类模块")
@RequiredArgsConstructor
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;


    @GetMapping(value = "/list/{parentId}")
    @ApiOperation(value = "根据分页获取商品分类列表")
    public R<Pager<ProductCategoryResp>> list(
            @PathVariable Long parentId,
            @RequestParam(value = "keyword", required = false) @ApiParam(name = "keyword", value = "关键字") String keyword,
            @RequestParam(value = "pageSize", defaultValue = "5") @ApiParam(name = "pageSize", value = "每页多少条", required = true) Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") @ApiParam(name = "pageNum", value = "当前第几页(从第一页开始)", required = true) Integer pageNum
    ) {

        return R.success(productCategoryService.list(parentId, keyword, pageSize, pageNum));
    }


    @GetMapping("/list/withChildren")
    @ApiOperation(value = "获取商品一级分类和二级分类的下拉级联数据")
    public R getWithChildren() {

        List<ProductCateChildrenResp> list = productCategoryService.getWithChildren();
        return R.success(list);
    }


    @PostMapping(value = "/update/navStatus")
    @ApiOperation(value = "修改导航栏显示状态")
    public R updateNavStatus(
            @RequestParam(value = "ids") @ApiParam(value = "ids", name = "ids") List<Long> ids,
            @RequestParam(value = "navStatus") @ApiParam(value = "navStatus", name = "显示状态：0->不显示；1->显示") Integer navStatus
    ) {

        boolean result = productCategoryService.updateNavStatus(ids, navStatus);
        return R.success(result);
    }

    @PostMapping(value = "/update/showStatus")
    @ApiOperation(value = "修改是否显示")
    public R updateShowStatus(
            @RequestParam(value = "ids") @ApiParam(value = "ids", name = "ids") List<Long> ids,
            @RequestParam(value = "showStatus") @ApiParam(value = "showStatus", name = "显示状态：0->不显示；1->显示") Integer showStatus
    ) {

        boolean result = productCategoryService.updateShowStatus(ids, showStatus);
        return R.success(result);
    }

    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "商品分类删除")
    public R delete(@PathVariable Long id) {

        boolean result = productCategoryService.delete(id);
        return R.success(result);
    }

    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "商品分类修改")
    public R update(
            @RequestBody ProductCategoryUpdReq productCategoryUpdReq) {

        boolean result = productCategoryService.update(productCategoryUpdReq);
        if (result) {
            return R.success(result);
        } else {
            return R.failed();
        }
    }

    @PostMapping(value = "/create")
    @ApiOperation(value = "添加商品分类")
    public R create(@RequestBody ProductCategoryAddReq productCategoryAddReq) {

        boolean result = productCategoryService.create(productCategoryAddReq);
        if (result) {
            return R.success(result);
        } else {
            return R.failed();
        }
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据ID获取商品分类信息")
    public R<ProductCategoryResp> getById(@PathVariable Long id) {

        ProductCategoryResp productCategory = productCategoryService.getInfoById(id);
        return R.success(productCategory);
    }


}

